//
//  HomeDataProvider.swift
//  ManBetX
//
//  Created by Cocoa on 2020/3/19.
//  Copyright © 2020 Fullen. All rights reserved.
//

import UIKit

/**
 * 本类处理Home页 具体业务网络请求
 * Success:返回数据模型 ViewModel 或者 容器<ViewModel>
 * Failure:统一返回失败信息
 **/



class HomeDataProvider: NSObject {

//    func fetchHomePageData(target: Int, success: (NSMutableArray) -> (Void), failed: (Int) -> (Void)){
//
//    }

    // 获取推荐游戏数据
    static func fetchRecommendGameData() -> NSMutableArray {
        let recommendGameInfoArray: NSMutableArray = NSMutableArray.init()
        if GlobalInfo.recommendGame != nil {
            let dataArray: [[String : Any]] = GlobalInfo.recommendGame!
            for tempDic:[String: Any] in dataArray {
                
                let viewModel = HomeSegmentViewModel.init(entity: tempDic)
                viewModel.create()
                recommendGameInfoArray.add(viewModel)
            }
        }
        return recommendGameInfoArray;
    }
    
    // 获取所有游戏数据
    static func fetchAllGameData() -> NSMutableArray {
        let allGameInfoArray: NSMutableArray = NSMutableArray.init()
        
        let dataArray: [[String : Any]] = GlobalInfo.platformInfoData!
        
        for tempDic:[String: Any] in dataArray {
            let listGameInfoArray: NSMutableArray = NSMutableArray.init()
        
            let gameList: [[String:Any]] = (tempDic["game_list"] as? [[String:Any]])!
            
            var index = 0
            for listDic:[String: Any] in gameList {
                
                let viewModel = HomeSegmentViewModel.init(entity: listDic)
                viewModel.create()
                if index == gameList.count-1 , gameList.count % 2 == 1 {
                    viewModel.isFirstItemAllAllLine = true
                }
                listGameInfoArray.add(viewModel)
                index = index + 1
            }
            allGameInfoArray.add(listGameInfoArray)
        }
        return allGameInfoArray;
    }
    // 获取所有游戏数据 - 分类标题
    static func fetchAllGameTitleData() -> NSMutableArray {
        let allGameTitleArray: NSMutableArray = NSMutableArray.init()
        
        let dataArray: [[String : Any]] = GlobalInfo.platformInfoData!
        for tempDic:[String: Any] in dataArray {
            
            let title = tempDic["f_name"] as? String ?? ""
            if title.count > 0 {
                allGameTitleArray.add(title)
            }
        }
        return allGameTitleArray;
    }
    
    // 获取游戏大厅数据 -- 决定各个游戏的状态
    static func fetchHallInfo(succeed succeedBlock: @escaping() -> Void,failed failedBlock: @escaping() -> Void){
        
        RequestHelper.get(atPath: "/kz/gp/v1/halls", withCompletion: { data in
            let hallInfoArray = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
            
//            print("首页游戏平台hallInfo信息:\(hallInfoArray)")
            let code = hallInfoArray["code"] as! Int
            let halls = hallInfoArray["data"] as! [[String:Any]]
            DispatchQueue.main.async {
                if code == 0{
                    // 游戏大厅数据
                    GlobalInfo.hallInfoData = halls
                    succeedBlock()
                }else if code == 999 || code == 1150{
                    // 获取数据有问题
                    failedBlock()
                }else {
                    // 获取数据有问题 -- 相当于获取失败
                    failedBlock()
                }
            }
        }, withError: { error in
            failedBlock()
        })
    }
}
